#include "stdafx.h"
#include "progressBar1.h"

class progressBar1 : public QWidget
{
    Q_OBJECT

public:
    progressBar1(QWidget* parent = nullptr) : QWidget(parent)
    {
        setupUI();
        setupConnections();
    }

private slots:
    void startProgress()
    {
        // 重置进度
        currentProgress = 0;
        progressBar->setValue(0);
        statusLabel->setText("任务进行中...");
        startButton->setEnabled(false);
        resetButton->setEnabled(true);

        // 开始定时器模拟任务进度
        timer->start(100); // 每100ms更新一次
    }

    void resetProgress()
    {
        timer->stop();
        currentProgress = 0;
        progressBar->setValue(0);
        statusLabel->setText("准备就绪");
        startButton->setEnabled(true);
        resetButton->setEnabled(false);
    }

    void updateProgress()
    {
        currentProgress += 2;
        progressBar->setValue(currentProgress);

        // 更新状态文本
        statusLabel->setText(QString("处理中... %1%").arg(currentProgress));

        // 检查是否完成
        if (currentProgress >= 100) {
            timer->stop();
            statusLabel->setText("任务完成!");
            startButton->setEnabled(true);
            resetButton->setEnabled(true);
            QMessageBox::information(this, "完成", "任务执行成功!");
        }
    }

private:
    void setupUI()
    {
        // 设置窗口属性
        setWindowTitle("Qt 进度条示例 - QProgressBar");
        setFixedSize(400, 200);

        // 创建布局
        QVBoxLayout* mainLayout = new QVBoxLayout(this);

        // 创建标题标签
        QLabel* titleLabel = new QLabel("嵌入式进度条演示");
        titleLabel->setAlignment(Qt::AlignCenter);
        QFont titleFont = titleLabel->font();
        titleFont.setPointSize(14);
        titleFont.setBold(true);
        titleLabel->setFont(titleFont);

        // 创建进度条
        progressBar = new QProgressBar();
        progressBar->setRange(0, 100);
        progressBar->setValue(0);
        progressBar->setFormat("%p%"); // 显示百分比
        progressBar->setTextVisible(true);

        // 状态标签
        statusLabel = new QLabel("准备就绪");
        statusLabel->setAlignment(Qt::AlignCenter);

        // 按钮
        startButton = new QPushButton("开始任务");
        resetButton = new QPushButton("重置");
        resetButton->setEnabled(false);

        QHBoxLayout* buttonLayout = new QHBoxLayout();
        buttonLayout->addWidget(startButton);
        buttonLayout->addWidget(resetButton);

        // 添加到主布局
        mainLayout->addWidget(titleLabel);
        mainLayout->addSpacing(20);
        mainLayout->addWidget(progressBar);
        mainLayout->addWidget(statusLabel);
        mainLayout->addSpacing(20);
        mainLayout->addLayout(buttonLayout);

        // 创建定时器
        timer = new QTimer(this);
    }

    void setupConnections()
    {
        connect(startButton, &QPushButton::clicked, this, &ProgressExample::startProgress);
        connect(resetButton, &QPushButton::clicked, this, &ProgressExample::resetProgress);
        connect(timer, &QTimer::timeout, this, &ProgressExample::updateProgress);
    }

    QProgressBar* progressBar;
    QPushButton* startButton;
    QPushButton* resetButton;
    QLabel* statusLabel;
    QTimer* timer;
    int currentProgress = 0;
};